package cn.sc.summer.calculate.service.impl;

import cn.sc.summer.constant.project.ProjectConstant;
import cn.sc.summer.constant.rabbit.RabbitConstant;
import cn.sc.summer.constant.redis.RedisConstant;
import cn.sc.summer.constant.util.AssertUtil;
import cn.sc.summer.exception.util.ip.IPUtil;
import cn.sc.summer.rabbitmq.builder.RabbitBuilder;
import cn.sc.summer.rabbitmq.builder.RabbitMessageBuilderDirector;
import cn.sc.summer.redis.util.NacosInstancesService;
import cn.sc.summer.redis.util.RedisHelper;
import cn.sc.summer.calculate.builder.SystemInfoBuilder;
import cn.sc.summer.calculate.builder.SystemInfoBuilderDirector;
import cn.sc.summer.calculate.po.SystemInfoDTO;
import cn.sc.summer.calculate.service.RefreshSystemInfoService;
import cn.sc.summer.thread.util.ThreadUtil;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * 类名：刷新系统基础信息
 *
 * @author a-xin
 * @date 2024/1/26 16:29
 */
@Slf4j
@Service
public class RefreshSystemInfoServiceImpl implements RefreshSystemInfoService {

    @Resource
    private ThreadUtil threadUtil;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private NacosConfigProperties nacosConfigProperties;

    @Resource
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    /**
     * 刷新系统基础信息
     */
    @Override
    public void refreshSystemInfo() {
        CompletableFuture.runAsync(() -> {
            AssertUtil.functionIf(NacosInstancesService.getInstanceNum(ProjectConstant.SERVER_NAME, nacosDiscoveryProperties) > 1, () -> {
                log.debug("==> Starts collecting basic system information from the server: {} ......", ProjectConstant.SERVER_NAME);
                SystemInfoDTO systemInfoDTO = new SystemInfoBuilderDirector(new SystemInfoBuilder()).build();
                systemInfoDTO.setServerName(ProjectConstant.SERVER_NAME);
                systemInfoDTO.setServerIp(IPUtil.getNacosInstanceIp());
                systemInfoDTO.setServerPort(Integer.parseInt(ProjectConstant.SERVER_PORT));
                systemInfoDTO.setServerCluster(nacosConfigProperties.getClusterName());
                systemInfoDTO.setServerNamespace(nacosConfigProperties.getNamespace());

                JSONObject message = new RabbitMessageBuilderDirector(new RabbitBuilder()).build();
                message.put(RabbitConstant.MESSAGE_KEY, JSONObject.toJSONString(systemInfoDTO));

                rabbitTemplate.convertAndSend(RabbitConstant.SERVER_SYSTEM_INFO_EXCHANGE,
                        RabbitConstant.SERVER_SYSTEM_INFO_ROUTING_KEY,
                        message.toJSONString());
            });
        }, threadUtil.THREAD_POOL());
    }

    /**
     * 删除系统基础信息
     */
    @Override
    public void deleteSystemInfo() {
        RedisHelper.del(RedisConstant.SERVER_SYSTEM_INFO + nacosConfigProperties.getNamespace() + ":" + NacosInstancesService.getServerNacosAddress());
        RedisHelper.zRemoveMember(RedisConstant.SERVER_SCORE + ProjectConstant.SERVER_NAME + ":" + nacosConfigProperties.getNamespace(), NacosInstancesService.getServerNacosAddress());
    }

}
